package com.sskj.cndragon.ui.activity;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.InstrumentationInfo;
import android.os.Environment;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.cndragon.R;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.presenter.AddReceiptActivityActivityPresenter;
import com.sskj.cndragon.view.SelectPictureDialog;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.box.inputfilter.IntNoZoreInputFilter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;

import java.io.File;
import java.util.List;

import butterknife.BindView;


public class AddReceiptActivityActivity extends BaseActivity<AddReceiptActivityActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_img)
    TextView tvImg;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.constraint)
    ConstraintLayout constraint;
    @BindView(R.id.iv_picture)
    ImageView ivPicture;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    private String type;
    private String account;
    private String img;
    private String mImagePath;
    private SelectPictureDialog selectPictureDialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_add_receipt;
    }

    @Override
    protected void initView() {
        super.initView();
        toolbar.setLeftButtonOnClickListener(View -> finish());
        type = getIntent().getStringExtra("type");
        account = getIntent().getStringExtra("account");
        img = getIntent().getStringExtra("img");
        if (type.equals(Constans.ALIPAY)) {
            toolbar.setTitle("添加支付宝");
            tvAccount.setText("支付宝账户");
            etAccount.setHint("请输入支付宝账户");
            tvImg.setText("点击添加支付宝收款二维码");
        } else if (type.equals(Constans.WECHAT)) {
            toolbar.setTitle("添加微信");
            tvAccount.setText("微信账户");
            etAccount.setHint("请输入微信账户");
            tvImg.setText("点击添加微信收款二维码");
        }
        if (!isEmpty(account)) {
            etAccount.setText(account);
        }
        if (!isEmpty(img)) {
            mImagePath = img;
            constraint.setVisibility(View.INVISIBLE);
            ivPicture.setVisibility(View.VISIBLE);
            ImageUtil.setImage(HttpConfig.BASE_URL + img, ivPicture);
        }
    }

    @Override
    protected void initData() {
        super.initData();
        ClickUtil.click(300, ivPicture, () -> {
            new RxPermissions(AddReceiptActivityActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
        ClickUtil.click(300, constraint, () -> {
            new RxPermissions(AddReceiptActivityActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(etAccount)) {
                ToastUtil.showShort(etAccount.getHint());
                return;
            }
            if (isEmpty(mImagePath)) {
                ToastUtil.showShort("请上传收款二维码");
                return;
            }
            if (isEmpty(etPayPwd)) {
                ToastUtil.showShort(etPayPwd.getHint());
                return;
            }
            mPresenter.setShouKuanWay(SPUtil.get(SPConfig.ID, ""), type, getText(etAccount), mImagePath,getText(etPayPwd));
        });
    }

    @Override
    public AddReceiptActivityActivityPresenter getPresenter() {
        return new AddReceiptActivityActivityPresenter();
    }


    public void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(AddReceiptActivityActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(AddReceiptActivityActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadFile(new File(mImagePath));
        }
    }

    public void uploadSuccess(String headUrl) {
        constraint.setVisibility(View.INVISIBLE);
        ivPicture.setVisibility(View.VISIBLE);
        mImagePath = headUrl;
        ImageUtil.setImage(HttpConfig.BASE_URL + headUrl, ivPicture);
    }

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, AddReceiptActivityActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    public static void start(Context context, String type, String account, String img) {
        Intent intent = new Intent(context, AddReceiptActivityActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("account", account);
        intent.putExtra("img", img);
        context.startActivity(intent);
    }


}
